package com.easy.service.Impl;

import com.easy.bean.Harvest;
import com.easy.mapper.HarvestMapper;
import com.easy.service.HarvestService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true) // 默认只读
public class HarvestServiceImpl implements HarvestService {

    @Autowired
    HarvestMapper harvestMapper;
    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Harvest add(Harvest harvest) {
        harvestMapper.add(harvest);

        // 使用回填的主键查询最新数据
        return harvestMapper.one(harvest.getHarvest_id());
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public int del(Integer id) {
        return harvestMapper.del(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Harvest edit(Harvest harvest) {
        harvestMapper.edit(harvest);
        Harvest result= harvestMapper.one(harvest.getHarvest_id());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public List<Harvest> all(Harvest harvest) {
        return harvestMapper.all(harvest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public Harvest one(Integer id) {
        return harvestMapper.one(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 写操作
    public void page(Page page, Harvest harvest) {
        int count=harvestMapper.pageCount(harvest);
        Object data=harvestMapper.page(page,harvest);
        page.setCount(count);
        page.setData(data);
    }
}
